<template>
  <div class="notice-management">
    <el-button type="success" @click="fetchNotices">刷新公告</el-button>
    <el-button type="danger" :disabled="selectedNotices.length === 0" @click="deleteSelectedNotices">批量删除</el-button>
    <el-button type="primary" @click="openAddNoticeDialog">添加公告</el-button>

    <el-table
      :data="notices"
      style="width: 100%; margin-top: 20px;"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="noticeTitle" label="公告标题" width="180" />
      <el-table-column label="公告内容">
        <template #default="scope">
          <el-tooltip effect="dark" :content="scope.row.noticeContent" placement="top">
            <div class="notice-content" @click="showFullContent(scope.row.noticeContent)">
              {{ scope.row.noticeContent }}
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="noticeAdminId" label="发布管理员" />
      <el-table-column prop="createTime" label="创建时间" width="180" />
      <el-table-column prop="updateTime" label="更新时间" width="180" />
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button type="primary" size="small" @click="editNotice(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑公告对话框 -->
    <el-dialog :title="dialogTitle" v-model="dialogVisible" width="40%">
      <el-form :model="noticeForm" :rules="rules" ref="noticeFormRef" label-width="100px">
        <el-form-item label="公告标题" prop="noticeTitle">
          <el-input v-model="noticeForm.noticeTitle" />
        </el-form-item>
        <el-form-item label="公告内容" prop="noticeContent">
          <el-input type="textarea" v-model="noticeForm.noticeContent" />
        </el-form-item>
        <el-form-item label="管理员" prop="noticeAdminId">
          <el-input v-model="noticeForm.noticeAdminId" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 公告内容弹窗 -->
    <el-dialog title="公告内容" v-model="fullContentVisible" width="40%">
      <div v-html="fullContent"></div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import http from '../utils/axios';
import { ElMessage, ElMessageBox } from 'element-plus';

const notices = ref([]);
const selectedNotices = ref([]);
const dialogVisible = ref(false);
const dialogTitle = ref('添加公告');
const noticeForm = ref({
  noticeId: null,
  noticeTitle: '',
  noticeContent: '',
  noticeAdminId: ''
});
const rules = ref({
  noticeTitle: [{ required: true, message: '请输入公告标题', trigger: 'blur' }],
  noticeContent: [{ required: true, message: '请输入公告内容', trigger: 'blur' }],
  noticeAdminId: [{ required: true, message: '请输入发布管理员', trigger: 'blur' }]
});
const noticeFormRef = ref(null);
const fullContentVisible = ref(false);
const fullContent = ref('');

const fetchNotices = async () => {
  try {
    const response = await http.get('/notice/all');
    notices.value = response.data;
  } catch (error) {
    ElMessage.error('获取公告列表时发生异常');
  }
};

const handleSelectionChange = (val) => {
  selectedNotices.value = val;
};

const deleteSelectedNotices = async () => {
  ElMessageBox.confirm('此操作将永久删除选中的公告, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      for (const notice of selectedNotices.value) {
        await http.delete(`/notice/${notice.noticeId}`);
      }
      await fetchNotices();
      ElMessage.success('批量删除公告成功');
    } catch (error) {
      ElMessage.error('批量删除公告时发生异常');
    }
  }).catch(() => {
    ElMessage.info('已取消删除');
  });
};

const editNotice = (notice) => {
  dialogTitle.value = '编辑公告';
  noticeForm.value = { ...notice };
  dialogVisible.value = true;
};

const openAddNoticeDialog = () => {
  dialogTitle.value = '添加公告';
  noticeForm.value = {
    noticeId: null,
    noticeTitle: '',
    noticeContent: '',
    noticeAdminId: ''
  };
  dialogVisible.value = true;
};

const submitForm = () => {
  noticeFormRef.value.validate(async (valid) => {
    if (valid) {
      try {
        if (noticeForm.value.noticeId) {
          // 更新公告
          await http.put(`/notice/${noticeForm.value.noticeId}`, noticeForm.value);
          ElMessage.success('公告更新成功');
        } else {
          // 添加公告
          await http.post('/notice/create', noticeForm.value);
          ElMessage.success('公告添加成功');
        }
        dialogVisible.value = false;
        await fetchNotices();
      } catch (error) {
        ElMessage.error('操作公告时发生异常');
      }
    } else {
      return false;
    }
  });
};

const showFullContent = (content) => {
  fullContent.value = content;
  fullContentVisible.value = true;
};

onMounted(() => {
  fetchNotices();
});
</script>

<style scoped>
.notice-management {
  padding: 20px;
}

.notice-content {
  white-space: pre-wrap; /* 保留空白符序列 */
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3; /* 限制显示行数 */
  line-clamp: 3;
  -webkit-box-orient: vertical;
  cursor: pointer;
}
</style>
